package net.controller;


import net.common.Msg;
import net.pojo.Category;
import net.pojo.User;
import net.service.CategoryService;
import net.service.UserService;
import net.utils.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/27 11:30
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    CategoryService categoryService;
    @RequestMapping(value = "/register",method= RequestMethod.POST)
    public String register(User user, Model model){
//        user.setPASSWORD(DigestUtils.md5DigestAsHex(user.getPASSWORD().getBytes()));
        user.setRegistTime(new Date());
        int i = userService.register(user);
        model.addAttribute("user",user);
        return "/client/registersuccess.jsp";
    }

    @RequestMapping("/login")
    public String login(String username,String PASSWORD,Model model,HttpSession session){
//        bug:用加密之后就有问题了

//            user.setPASSWORD(DigestUtils.md5DigestAsHex(user.getPASSWORD().getBytes()));

        User login = userService.login(username,PASSWORD);
        if (login != null){
            session.setAttribute("user",login);
            return "redirect:/main/index.do";
        } else {
            List<Category> list = categoryService.findAll();
            model.addAttribute("list", list);
            model.addAttribute("msg","用户名或密码错误!");
            return "/client/login.jsp";
        }
    }
    @RequestMapping("/myaccount")
    public String myaccount(Model model){
        List<Category> list = categoryService.findAll();
        model.addAttribute("list", list);
        return "/client/myAccount.jsp";
    }
    @RequestMapping("/loginout")
    public String loginout(HttpSession session){
        session.invalidate();
        return "redirect:/main/index.do";
    }

    @RequestMapping("/checkUser")
    @ResponseBody
    public Msg checkUser(User user){
        int checkUser = userService.checkUser(user.getUsername());
        Msg msg = new Msg();
        if (checkUser==0){
            msg.setFlag(true);
        }else {
            msg.setFlag(false);
        }
        return msg;
    }

    @RequestMapping("/checkEmails")
    @ResponseBody
    public Msg checkEmails(User user){
        int checkEmails = userService.checkEmails(user.getEmail());
        Msg msg = new Msg();
        if (checkEmails==0){
            msg.setFlag(true);
        }else {
            msg.setFlag(false);
        }
        return msg;
    }
    @RequestMapping("/code")
    public void code(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Code.outputCaptcha(request, response);
    }





}
